﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gChartSharp
{
    /// <summary>
    /// http://code.google.com/apis/chart/#grid
    /// </summary>
    public class ChartGridLines : IChartParam
    {
        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chg"; } }


        /// <summary>
        /// XAxis step size
        /// </summary>
        public int XAxisStepSize { get; set; }


        /// <summary>
        /// YAxis step size
        /// </summary>
        public int YAxisStepSize { get; set; }


        /// <summary>
        /// Length of line segment - Rounded to 1 decimal place
        /// </summary>
        /// 
        /// <remarks>
        /// The API only allows one decimal place which 
        /// is why we are rounding here.  In addition, since this 
        /// is an optional property let's make the member variable
        /// nullable so we can determine whether or not it has been
        /// initialized when the Render() method is called.  This 
        /// is hidden from the outside by coalescing the get to 
        /// return 0 if it is null.
        /// </remarks>
        public decimal LineSegmentLength
        {
            get { return mLineSegmentLength ?? 0M; }
            set { mLineSegmentLength = Math.Round(value, 1); }
        }
        private decimal? mLineSegmentLength = null;


        /// <summary>
        /// Length of blank segment - Rounded to 1 decimal place
        /// </summary>
        public decimal BlankSegmentLength 
        {
            get { return mBlankSegmentLength ?? 0M; }
            set { mBlankSegmentLength = Math.Round(value, 1); }
        }
        private decimal? mBlankSegmentLength = null;


        /// <summary>
        /// Constructor to initialize required properties
        /// </summary>
        /// <param name="xAxisStepSize"></param>
        /// <param name="yAxisStepSize"></param>
        public ChartGridLines(int xAxisStepSize, int yAxisStepSize)
        {
            this.XAxisStepSize = xAxisStepSize;
            this.YAxisStepSize = yAxisStepSize;
        }


        /// <summary>
        /// Constructor to initialize all properties
        /// </summary>
        /// <param name="xAxisStepSize"></param>
        /// <param name="yAxisStepSize"></param>
        /// <param name="lineSegmentLength"></param>
        /// <param name="blankSegmentLength"></param>
        public ChartGridLines(int xAxisStepSize, int yAxisStepSize, decimal lineSegmentLength, decimal blankSegmentLength)
        {
            this.XAxisStepSize = xAxisStepSize;
            this.YAxisStepSize = yAxisStepSize;
            this.LineSegmentLength = lineSegmentLength;
            this.BlankSegmentLength = blankSegmentLength;
        }

        // hide the default constructor
        private ChartGridLines() { }


        /// <summary>
        /// Render the API text
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.XAxisStepSize);
            sb.Append(",");
            sb.Append(this.YAxisStepSize);

            // if these optional properties were set, add them
            if (mLineSegmentLength != null && mBlankSegmentLength != null)
            {
                sb.Append(",");
                sb.Append(mLineSegmentLength);
                sb.Append(",");
                sb.Append(mBlankSegmentLength);
            }

            return sb.ToString();
        }
    }
}
